﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aspfm.Dal;
using Aspfm.Main;

namespace Aspfm.DataBase
{
    public partial class UcSqlPage : UserControl
    {
        public UcSqlPage()
        {
            InitializeComponent();

            comboBox1.Items.Add("Oracle");
            comboBox1.Items.Add("MSSql");
            comboBox1.Items.Add("SQLite");

            comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
            comboBox1.SelectedItem = "Oracle";
            Init();
        }

        void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Init();
        }

        public void Init()
        {
            textBox1.Text = "";
            string datatype = comboBox1.SelectedItem.ToString();
            if (Form1.datalist != null && Form1.datalist.pageSql != null)
            {
                if (datatype == "Oracle")
                {
                    if (Form1.datalist.pageSql.ContainsKey(DataSourceType.Oracle))
                    {
                        textBox1.Text = Form1.datalist.pageSql[DataSourceType.Oracle];
                    }
                }
                else if (datatype == "MSSql")
                {
                    if (Form1.datalist.pageSql.ContainsKey(DataSourceType.MSSql))
                    {
                        textBox1.Text = Form1.datalist.pageSql[DataSourceType.MSSql];
                    }
                }
                else if (datatype == "SQLite")
                {
                    if (Form1.datalist.pageSql.ContainsKey(DataSourceType.SQLite))
                    {
                        textBox1.Text = Form1.datalist.pageSql[DataSourceType.SQLite];
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Form1.datalist != null && Form1.datalist.pageSql != null)
            {
                if (comboBox1.SelectedItem.ToString() == "Oracle")
                {
                    Form1.datalist.pageSql[DataSourceType.Oracle] = textBox1.Text.Trim();
                }
                else if (comboBox1.SelectedItem.ToString() == "MSSql")
                {
                    Form1.datalist.pageSql[DataSourceType.MSSql] = textBox1.Text.Trim();
                }
                else if (comboBox1.SelectedItem.ToString() == "SQLite")
                {
                    Form1.datalist.pageSql[DataSourceType.SQLite] = textBox1.Text.Trim();
                }
                ConfigTool.SaveConn(Form1.datalist);
            }
        }
    }
}
